/*
 * FunctionCallExpression.java
 * 
 * last update: 01.02.2010 by Florea Mihai
 * 
 * author:	Power(power@kermit.cs.pub.ro)
 * 			Florea Mihai(florea.mihai@gmail.com)
 * 
 * Obs: am modificat liniile 21, 22, 29
 */

package engine;

import java.util.*;

class FunctionCallExpression extends Expression{

	/**
	 * The expressions of the actual parameters
	 */
	Vector<Object> paramExps;
	Vector<Object> paramInstances;
	DefaultInstruction theFunction;
	FunctionDescription  funcDescription;
	ExpressionDescription desc;

	FunctionCallExpression(NameSpace  ns, ExpressionDescription desc,
			FunctionDescription funcDescription, 
			Vector<Object> paramExps, Vector<Object> paramInstances,
			DefaultInstruction theFunction)
			{
		super(ns);
		this.desc = desc;
		this.funcDescription = funcDescription;
		this.paramExps = paramExps;
		this.paramInstances = paramInstances;
		this.theFunction = theFunction;
			}

	/**
	 * Adds a monitor on the result's placeholder.
	 */
	 void addMonitor(Monitor m){
		 xConsole.debug("param 0: " + paramInstances.elementAt(0));
		 (((Register)paramInstances.elementAt(0)).getData(0)).addMonitor(m);
	 }

	 public synchronized Result evaluate() throws InterpretTimeException{
		 xConsole.debug(this + " |->");
		 funcDescription.storeParamValues(paramExps, paramInstances);
		 ControlThread ct;
		 if (theFunction != null) {
			 ct = new ControlThread(theFunction,
					 Thread.currentThread().getThreadGroup());
			 //since functions cannot have timing control, we do not
			 //need to register this thread into Time's vector.
			 ct.start();
			 try {
				 ct.join();
			 } catch (InterruptedException intex) {}
		 }

		 xConsole.debug(this + " <-|");
		 Object o = paramInstances.elementAt(0);
		 if(o instanceof Register)
			 return ((Register)o).getData(0).duplicate();
		 return ((Result)o).duplicate();
	 }

	 public String toString(){
		 return desc.toString();
	 }
}



